﻿using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;

namespace WPFExcelReport
{

    public class FLTABMCOData : BaseFunction
    {
        DataRow dwIntexStd = null;
        public SETINTEXDB INTEXDB = new SETINTEXDB();
        public DataSet SetCAD;
        public DataSet SetExt
        {
            get
            {
                return Convert2Intex.SetExt;
            }
            set
            {
                Convert2Intex.SetExt = value;
            }
        }
        public Convert2Intex Convert2Intex;
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }

        public FLTABMCOData()
            : base()
        {
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        } 
        public DataSet LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet(); 
            return SReturn;
        }

        public DataSet LoadData(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = null;
            try
            {
                string client = dwInput["MANDANT"].ToString();
                string month = dwInput["MONTH"].ToString();
                ResetICC_MGABEW(client,month);
                dwData = dwInput;
                SResult = INTEXDBC.QueryICC_EVLCOST(dwData, 2);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }

        public DataSet LoadGB14(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = dwInput;
                //产生移动记录表
                EventDispatcher.Instance.RaiseStatus("产生移动记录表", StatusEventType.RunningProgress);
                dwData = INTEXDB.ICC_MGABEW.NewRow();
                dwData["GABMANDANT"] = dwInput["MANDANT"];
                dwData["GABLO"] = dwInput["LO"];
                dwData["GABART"] = dwInput["MART"];
                dwData["GABVAR"] = dwInput["MVAR"];
                dwData["GABFARBE"] = dwInput["MFARBE"];
                dwData["GABBUCHDAT"] = dwInput["MONTH"];              
                SResult = INTEXDBC.QueryICC_MGABEW(dwData, 2);
                SReturn.Merge(SResult); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet LoadDetail(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = dwInput;
                //产生移动记录表
                EventDispatcher.Instance.RaiseStatus("产生移动记录表", StatusEventType.RunningProgress);
                dwData = INTEXDB.ICC_MGABEW.NewRow();
                dwData["GABMANDANT"] = dwInput["MANDANT"];
                dwData["GABLO"] = dwInput["LO"];
                dwData["GABART"] = dwInput["FART"];
                SResult = INTEXDBC.QueryICC_MGABEW(dwData, 3);
                SResult.Tables[0].TableName = "ICC_MGABEWAB";
                SReturn.Merge(SResult);
                dwData = dwInput;
                SResult = INTEXDBC.QueryICC_ABCOST(dwData, 2);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryICC_EXSTOCK(dwData, 2);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        private void ResetICC_MGABEW(string client, string month,int option=0)
        { 
            DataSet SResult = new DataSet();
            DataRow dwData = null;
            dwData = INTEXDB.ICC_MGABEW.NewRow();
            dwData["GABMANDANT"] = client;
            dwData["GABBUCHDAT"] = month;
            SResult = INTEXDBC.QueryICC_MGABEW(dwData, 4);
            if (SResult.Tables[0].Rows.Count == 0
                ||option==1)
            {
                INTEXDBC.DeleteICC_MGABEW(dwData, 1);
                INTEXDBC.InsertICC_MGABEW(dwData);
                INTEXDBC.DeleteICC_MGABEW(dwData, 2);
                INTEXDBC.DeleteICC_MGABEW(dwData, 3);
                INTEXDBC.DeleteICC_MGABEW(dwData, 4);
                INTEXDBC.UpdateICC_MGABEW(dwData, 2);
            }
            INTEXDBC.InsertICC_ZUTEIEX(dwData, 3);//对应品种的发料
            INTEXDBC.InsertICC_ZUTEIEX(dwData, 4);//对应品种的扣料
        }
        private void ReSetStock(DataRow dwInput)
        {
            DataRow dwData = null;
            string SetDate = "";
            dwData = INTEXDB.ICC_EXSTOCK.NewRow();
            dwData["MANDANT"] = dwInput["MANDANT"];
            dwData["MONTH"] = dwInput["MONTH"];
            //所有库存条目，期初期末均为0
            EventDispatcher.Instance.RaiseStatus("所有库存条目，期初期末均为0", StatusEventType.RunningProgress);
            INTEXDBC.DeleteICC_EXSTOCK(dwData, 2);
            INTEXDBC.InsertICC_EXSTOCK(dwData, 3);
            INTEXDBC.InsertICC_EXSTOCK(dwData, 4);//根据ICC_MGABEW补充一些记录
            //更新期初库存
            EventDispatcher.Instance.RaiseStatus("更新期初库存", StatusEventType.RunningProgress);
            SetDate = dwData["MONTH"].ToString().Trim() + (DateTime.DaysInMonth(Convert.ToInt16(Left(dwData["MONTH"].ToString(), 4)), Convert.ToInt16(Right(dwData["MONTH"].ToString().Trim(), 2)))).ToString();
            int Myear = Convert.ToInt16(Left(dwData["MONTH"].ToString(), 4));
            int MMonth = Convert.ToInt16(Right(dwData["MONTH"].ToString().Trim(), 2));
            if ((MMonth - 1) == 0)
            {
                dwData["SETDATE"] = (Myear - 1).ToString() + "12" + DateTime.DaysInMonth(Myear - 1, 12).ToString();
            }
            else
            {
                dwData["SETDATE"] = dwData["MONTH"].ToString().Trim() + "01";
            }
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 2);
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 21);
            INTEXDBC.InsertICC_ZUTEIEX(dwData, 1);//期初留位
            //更新期末库存
            EventDispatcher.Instance.RaiseStatus("更新期末库存", StatusEventType.RunningProgress);
            if ((MMonth+1) == 13)
            {
                dwData["SETDATE"] = (Myear +1).ToString() + "01" + DateTime.DaysInMonth(Myear +1, 1).ToString();
            }
            else
            {
                dwData["SETDATE"] = Myear.ToString() + Right("00" +(MMonth + 1).ToString(),2) +"01";
            }
            //SetDate = dwData["MONTH"].ToString().Trim() + DateTime.DaysInMonth(Myear, MMonth).ToString();
            //dwData["SETDATE"] = SetDate;
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 3);
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 31);
            INTEXDBC.InsertICC_ZUTEIEX(dwData, 2);//期末留位
            //更新增加数量
            EventDispatcher.Instance.RaiseStatus("更新增加数量", StatusEventType.RunningProgress);
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 4);
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 41);
            //更新生产之外的减少数量
            EventDispatcher.Instance.RaiseStatus("更新生产之外的减少数量", StatusEventType.RunningProgress);
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 5);
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 51);
            EventDispatcher.Instance.RaiseStatus("更新库存差异，期初库存+增加数量-期末库存", StatusEventType.RunningProgress);
            //更新正变动，增加数量-生产之外的减少数量=增加的数量
            //更新库存差异，期初库存+增加数量-期末库存
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 6);
            //更新正变动
            EventDispatcher.Instance.RaiseStatus("更新正变动", StatusEventType.RunningProgress);
            INTEXDBC.UpdateICC_EXSTOCK(dwData, 7);
        }
        private void ResetABCOST(DataRow dwInput)
        {
            DataRow dwData = null;
            string SetDate = "";
            dwData = INTEXDB.ICC_ABCOST.NewRow();
            dwData["MANDANT"] = dwInput["MANDANT"];
            dwData["MONTH"] = dwInput["MONTH"];
            INTEXDBC.DeleteICC_ABCOST(dwData, 2);
            //更新收集生产单对应纱线的消耗
            EventDispatcher.Instance.RaiseStatus("更新收集生产单对应纱线的消耗", StatusEventType.RunningProgress);
            INTEXDBC.InsertICC_ABCOST(dwData, 1);
            //更新总消耗
            EventDispatcher.Instance.RaiseStatus("更新总消耗", StatusEventType.RunningProgress);
            INTEXDBC.UpdateICC_ABCOST(dwData, 2);
            INTEXDBC.UpdateICC_ABCOST(dwData, 3);
            INTEXDBC.UpdateICC_ABCOST(dwData, 5);
            EventDispatcher.Instance.RaiseStatus("更新总产出", StatusEventType.RunningProgress);
            INTEXDBC.DeleteICC_YAB13(dwData, 2);
            INTEXDBC.InsertICC_YAB13(dwData, 1);
            INTEXDBC.UpdateICC_ABCOST(dwData, 4);
        }
        private void ResetEVLCOST(DataRow dwInput)
        {
            DataRow dwData = null;
            string SetDate = "";
            dwData = INTEXDB.ICC_EVLCOST.NewRow();
            dwData["MANDANT"] = dwInput["MANDANT"];
            dwData["MONTH"] = dwInput["MONTH"];
            //更新总消耗比例，分配消耗的纱线数量
            //产生矩阵
            EventDispatcher.Instance.RaiseStatus("更新总消耗比例，分配消耗的纱线数量", StatusEventType.RunningProgress);
            INTEXDBC.DeleteICC_EVLCOST(dwData, 2);
            INTEXDBC.InsertICC_EVLCOST(dwData, 1);
            INTEXDBC.UpdateICC_EVLCOST(dwData, 2);
            INTEXDBC.UpdateICC_EVLCOST(dwData, 3);
            int Myear = Convert.ToInt16(Left(dwData["MONTH"].ToString(), 4));
            int MMonth = Convert.ToInt16(Right(dwData["MONTH"].ToString().Trim(), 2));
            if ((MMonth - 1) == 0)
            {
                dwData["SETDATE"] = (Myear - 1).ToString() + "12" + DateTime.DaysInMonth(Myear - 1, Myear - 1).ToString();
            }
            else
            {
                dwData["SETDATE"] = dwData["MONTH"].ToString().Trim() + "01";
            }
            
            INTEXDBC.UpdateICC_EVLCOST(dwData, 4);//期初库存按单
            if ((MMonth + 1) == 13)
            {
                dwData["SETDATE"] = (Myear + 1).ToString() + "01" + DateTime.DaysInMonth(Myear + 1, 1).ToString();
            }
            else
            {
                dwData["SETDATE"] = Myear.ToString() + Right("00" + (MMonth + 1).ToString(), 2) + "01";
            }
            INTEXDBC.UpdateICC_EVLCOST(dwData, 5);//期末库存按单
            INTEXDBC.UpdateICC_EVLCOST(dwData, 6);//接受原料按单
            INTEXDBC.UpdateICC_EVLCOST(dwData, 7); //发出原料按单
        }
        public DataSet BuildData( DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwData = null;
            string SetDate="";
            try
            {
                dwData = INTEXDB.ICC_EVLCOST.NewRow();
                dwData["MANDANT"] = dwInput["MANDANT"];
                dwData["MONTH"] = dwInput["MONTH"];

                //产生移动记录表
                EventDispatcher.Instance.RaiseStatus("产生移动记录表", StatusEventType.RunningProgress);
                string client = dwInput["MANDANT"].ToString();
                string month = dwInput["MONTH"].ToString();
                ResetICC_MGABEW(client, month,1);
                ReSetStock(dwInput);
                ResetABCOST(dwInput);

                ResetEVLCOST(dwInput);                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        } 
    }
}
